package com.lgt_enterprise_android.view;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.FrameLayout;

import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.view.pages.b.ArrangeJobActivity;
import com.lgt_enterprise_android.view.pages.b.InviteStaffActivity;
import com.util.AppUtil;

public class MainActivityManager {

    private Context mContext;

    private float startX;
    private float startY;


    private FrameLayout frameAnim;

    private View mImgMiddle;
    private View mImgMiddle1;
    private View mImgMiddle2;
//    private View mImgMiddle3;
    private View mImgMiddleDisapper;

    public void bind(Context _context){
        mContext=_context;

        Activity activity=(Activity) mContext;

        frameAnim=activity.findViewById(R.id.frame_anim);

        mImgMiddle=activity.findViewById(R.id.tab_img_middle);
        mImgMiddle1=activity.findViewById(R.id.tab_img_middle1);
        mImgMiddle2=activity.findViewById(R.id.tab_img_middle2);
//        mImgMiddle3=activity.findViewById(R.id.tab_img_middle3);
        mImgMiddleDisapper=activity.findViewById(R.id.tab_img_middle_disapper);
        mImgMiddle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startAnim(true);
            }
        });

        mImgMiddleDisapper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startAnim(false);
            }
        });

        mImgMiddle1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, InviteStaffActivity.class);
                mContext.startActivity(intent);
                frameAnim.setVisibility(View.GONE);
            }
        });

        mImgMiddle2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, ArrangeJobActivity.class);
                intent.putExtra("type",2);
                mContext.startActivity(intent);
                frameAnim.setVisibility(View.GONE);
            }
        });



    }

    public  void startAnim(boolean flag) {
        startX = mImgMiddle.getTranslationX();
        startY = mImgMiddle.getTranslationY()-50;

        //x轴偏移量
        float endX_menu1 = AppUtil.getPixelWidth(mContext) / 4;
        //Y轴坐标 向上偏移量300px  可自己根据dp转换成px
        float endY_menu1 = startY - 300;

        if (flag) {
            frameAnim.setVisibility(View.VISIBLE);

            //点击按钮的旋转动画
            ObjectAnimator rotation = ObjectAnimator.ofFloat(mImgMiddle, "rotation", 0, 270);
            rotation.setDuration(500);
            rotation.start();

            //分别设置x轴Y轴的偏移量
            ObjectAnimator menu1_animatorX = ObjectAnimator.ofFloat(mImgMiddle1, "translationX", startX, endX_menu1);
            ObjectAnimator menu1_animatorY = ObjectAnimator.ofFloat(mImgMiddle1, "translationY", startY, endY_menu1);
            //缩放动画
            ObjectAnimator menu1_animator_scaleX = ObjectAnimator.ofFloat(mImgMiddle1, "scaleX", 0f, 1f);
            ObjectAnimator menu1_animator_scaleY = ObjectAnimator.ofFloat(mImgMiddle1, "scaleY", 0f, 1f);

            AnimatorSet menu1Set = new AnimatorSet();
            menu1Set.setDuration(500);
            //OvershootInterpolator插值器实现动画抖动效果
            menu1Set.setInterpolator(new OvershootInterpolator());
            menu1Set.playTogether(menu1_animatorX, menu1_animatorY, menu1_animator_scaleX, menu1_animator_scaleY);
            menu1Set.start();


            ObjectAnimator menu2_animatorX = ObjectAnimator.ofFloat(mImgMiddle2, "translationX", startX, -endX_menu1);
            ObjectAnimator menu2_animatorY = ObjectAnimator.ofFloat(mImgMiddle2, "translationY", startY, endY_menu1);
            ObjectAnimator menu2_animator_scaleX = ObjectAnimator.ofFloat(mImgMiddle2, "scaleX", 0f, 1f);
            ObjectAnimator menu2_animator_scaleY = ObjectAnimator.ofFloat(mImgMiddle2, "scaleY", 0f, 1f);
            AnimatorSet menu2Set = new AnimatorSet();
            menu2Set.setDuration(500);
            menu2Set.setInterpolator(new OvershootInterpolator());
            menu2Set.playTogether(menu2_animatorX, menu2_animatorY, menu2_animator_scaleX, menu2_animator_scaleY);
            menu2Set.start();


//            ObjectAnimator menu3_animatorX = ObjectAnimator.ofFloat(mImgMiddle3, "translationX", startX, -startX);
//            ObjectAnimator menu3_animatorY = ObjectAnimator.ofFloat(mImgMiddle3, "translationY", startY, endY_menu1);
//            ObjectAnimator menu3_animator_scaleX = ObjectAnimator.ofFloat(mImgMiddle3, "scaleX", 0f, 1f);
//            ObjectAnimator menu3_animator_scaleY = ObjectAnimator.ofFloat(mImgMiddle3, "scaleY", 0f, 1f);
//            AnimatorSet menu3Set = new AnimatorSet();
//            menu3Set.setDuration(500);
//            menu3Set.setInterpolator(new OvershootInterpolator());
//            menu3Set.playTogether(menu3_animatorX, menu3_animatorY, menu3_animator_scaleX, menu3_animator_scaleY);
//            menu3Set.start();


        } else {
            ObjectAnimator rotation = ObjectAnimator.ofFloat(mImgMiddleDisapper, "rotation", 270, 0);
            rotation.setDuration(500);
            rotation.start();

            ObjectAnimator menu1_animatorX = ObjectAnimator.ofFloat(mImgMiddle1, "translationX", endX_menu1, startX);
            ObjectAnimator menu1_animatorY = ObjectAnimator.ofFloat(mImgMiddle1, "translationY", endY_menu1, startY);

            ObjectAnimator menu1_animator_scaleX = ObjectAnimator.ofFloat(mImgMiddle1, "scaleX", 1f, 0f);
            ObjectAnimator menu1_animator_scaleY = ObjectAnimator.ofFloat(mImgMiddle1, "scaleY", 1f, 0f);

            AnimatorSet menu1Set = new AnimatorSet();
            menu1Set.setDuration(500);
            menu1Set.playTogether(menu1_animatorX, menu1_animatorY, menu1_animator_scaleX, menu1_animator_scaleY);
            menu1Set.start();


            ObjectAnimator menu2_animatorX = ObjectAnimator.ofFloat(mImgMiddle2, "translationX", -endX_menu1, startX);
            ObjectAnimator menu2_animatorY = ObjectAnimator.ofFloat(mImgMiddle2, "translationY", endY_menu1, startY);
            ObjectAnimator menu2_animator_scaleX = ObjectAnimator.ofFloat(mImgMiddle2, "scaleX", 1f, 0f);
            ObjectAnimator menu2_animator_scaleY = ObjectAnimator.ofFloat(mImgMiddle2, "scaleY", 1f, 0f);
            AnimatorSet menu2Set = new AnimatorSet();
            menu2Set.setDuration(500);
            menu2Set.playTogether(menu2_animatorX, menu2_animatorY, menu2_animator_scaleX, menu2_animator_scaleY);
            menu2Set.start();

            menu2Set.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    frameAnim.setVisibility(View.GONE);
                }
            });

//            ObjectAnimator menu3_animatorX = ObjectAnimator.ofFloat(mImgMiddle3, "translationX", startX, startX);
//            ObjectAnimator menu3_animatorY = ObjectAnimator.ofFloat(mImgMiddle3, "translationY", endY_menu1, startY);
//            ObjectAnimator menu3_animator_scaleX = ObjectAnimator.ofFloat(mImgMiddle3, "scaleX", 1f, 0f);
//            ObjectAnimator menu3_animator_scaleY = ObjectAnimator.ofFloat(mImgMiddle3, "scaleY", 1f, 0f);
//            AnimatorSet menu3Set = new AnimatorSet();
//            menu3Set.setDuration(500);
//            menu3Set.playTogether(menu3_animatorX, menu3_animatorY, menu3_animator_scaleX, menu3_animator_scaleY);
//            menu3Set.start();
//
//            menu3Set.addListener(new AnimatorListenerAdapter() {
//                @Override
//                public void onAnimationEnd(Animator animation) {
//                    super.onAnimationEnd(animation);
//                    frameAnim.setVisibility(View.GONE);
//                }
//            });


        }


    }


}
